/***********************************************************************************************************************
    @file    adc_normalchannel_singleconversion_polling.c
    @author  FAE Team
    @date    15-Mar-2023
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2023> <MindMotion></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/

/* Define to prevent recursive inclusion */
#define _ADC_NORMALCHANNEL_SINGLECONVERSION_POLLING_C_

/* Files include */
#include <stdio.h>
#include "platform.h"
#include "AdSample.h"
#include "Control.h"
#include "Timer1.h"

/**
 * @addtogroup MM32SPIN0230_LibSamples
 * @{
 */

/**
 * @addtogroup ADC
 * @{
 */

/**
 * @addtogroup ADC_NormalChannel_SingleConversion_Polling
 * @{
 */

/* Private typedef ****************************************************************************************************/

/* Private define *****************************************************************************************************/

/* Private macro ******************************************************************************************************/

/* Private variables **************************************************************************************************/

ADSample_st st_RoomSnr; // 冷室温度
uint8_t u8_ADSampleCnt = 0;
uint16_t u16_AdcValue[11];
uint16_t u16_stADRoomTemp = 2801;
signed char s8_RealTemp = 0;

const uint16_t ADRoomTemp[] = {
    4095,                                                                                                                   // 开路
    3782, 3772, 3762, 3751, 3741, 3730, 3718, 3707, 3695, 3683, 3671, 3658, 3645, 3632, 3618, 3605, 3590, 3576, 3561, 3546, //-50--41
    3531, 3515, 3499, 3483, 3467, 3450, 3433, 3415, 3397, 3379, 3361, 3342, 3323, 3304, 3284, 3303, 3244, 3223, 3202, 3181, //-40--31
    3160, 3138, 3116, 3094, 3071, 3048, 3025, 3002, 2978, 2954, 2930, 2906, 2882, 2857, 2832, 2806, 2781, 2755, 2730, 2704, //-30--21
    2678, 2652, 2625, 2599, 2572, 2545, 2518, 2492, 2464, 2437, 2410, 2383, 2356, 2329, 2301, 2274, 2247, 2219, 2192, 2165, //-20--11
    2137, 2110, 2083, 2055, 2028, 2001, 1974, 1948, 1921, 1894, 1868, 1842, 1815, 1789, 1764, 1738, 1712, 1687, 1662, 1637, //-10--1
    1612,                                                                                                                   // 0
    1587, 1563, 1539, 1515, 1491, 1467, 1444, 1421, 1398, 1375, 1353, 1331, 1309, 1288, 1266, 1245, 1224, 1204, 1184, 1164, // 1-10
    1144, 1125, 1105, 1086, 1068, 1049, 1031, 1013, 996, 978, 961, 944, 928, 911, 895, 879, 864, 849, 834, 819,             // 11-20
    804, 790, 776, 762, 748, 735, 722, 709, 696, 683, 671, 659, 647, 635, 624, 613, 602, 591, 580, 569,                     // 21-30
    559, 549, 539, 529, 520, 511, 501, 492, 483, 474, 466, 457, 449, 441, 433, 425, 418, 410, 403, 396,                     // 31-40
    389, 382, 375, 368, 361, 355, 349, 342, 336, 330, 324, 319, 313, 307, 302, 297, 291, 286, 281, 276,                     // 41-50
    272, 267, 262, 257, 253, 249, 244, 240, 236, 232, 228, 224, 220, 216, 213, 209, 205, 202, 198, 195,                     // 51-60
    192, 189, 185, 182, 179, 176, 173, 170, 168, 165, 162, 159, 157, 154, 152, 149, 147, 144, 142, 140,                     // 61-70
    137, 135, 133, 131, 129, 127, 125, 123, 121, 119,                                                                       // 71-75
    0                                                                                                                       // 短路
};

const signed char RealTemp[] = {
    0xFF,                                                                                               // 开路错误
    -50, -50, -49, -49, -48, -48, -47, -47, -46, -46, -45, -45, -44, -44, -43, -43, -42, -42, -41, -41, //-50--41
    -40, -40, -39, -39, -38, -38, -37, -37, -36, -36, -35, -35, -34, -34, -33, -33, -32, -32, -31, -31, //-40--30
    -30, -30, -29, -29, -28, -28, -27, -27, -26, -26, -25, -25, -24, -24, -23, -23, -22, -22, -21, -21, //-30--20
    -20, -20, -19, -19, -18, -18, -17, -17, -16, -16, -15, -15, -14, -14, -13, -13, -12, -12, -11, -11, //-20--10
    -10, -10, -9, -9, -8, -8, -7, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1,                   //-10--1
    0,                                                                                                  // 0
    1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,                                       // 1-10
    11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20,                     // 10-20
    21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30,                     // 20-30
    31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40,                     // 30-40
    41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50,                     // 40-50
    51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 58, 58, 59, 59, 60, 60,                     // 50-60
    61, 61, 62, 62, 63, 63, 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70,                     // 60-70
    71, 71, 72, 72, 73, 73, 74, 74, 75, 75,                                                             // 71-75
    0                                                                                                   // 短路错误值
};

#define AD_TEMP_TABLE_SIZE sizeof(ADRoomTemp) / 2
/* Private functions **************************************************************************************************/

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void ADC_Configure(void)
{
  ADC_InitTypeDef ADC_InitStruct;
  GPIO_InitTypeDef GPIO_InitStruct;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_ADC1, ENABLE);

  /* NTC1->PA5     */
  ADC_StructInit(&ADC_InitStruct);
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStruct.ADC_Prescaler = ADC_Prescaler_16;
  ADC_InitStruct.ADC_Mode = ADC_Mode_Imm;
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_Init(ADC1, &ADC_InitStruct);

  ADC_SampleTimeConfig(ADC1, ADC_Channel_4, ADC_SampleTime_240_5);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* NTC1->PA5  */
  GPIO_StructInit(&GPIO_InitStruct);
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStruct);

  ADC_Cmd(ADC1, ENABLE);
}

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void ADC_GetChannelTemp(uint8_t Channel)
{
  uint16_t Value = 0;

  ADC_ChannelCmd(ADC1, Channel, ENABLE);

  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  while (RESET == ADC_GetFlagStatus(ADC1, ADC_FLAG_EOS))
  {
  }

  ADC_ClearFlag(ADC1, ADC_FLAG_EOS);

  Value = ADC_GetConversionValue(ADC1);

  ADC_ChannelCmd(ADC1, Channel, DISABLE);
  u16_AdcValue[Channel & 0x0F] = Value;
}

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void ADC_SampleControl(void)
{
  uint8_t u8_TempTableIndex = 0;
  printf("\r\nTest %s", __FUNCTION__);

  // 20ms while (1)
  if ((uint32_t)(u32_systemTime - u32_GetADTimer) > GET_AD_COV_TIME)
  {
    u32_GetADTimer = u32_systemTime;
    // printf("\r\nRV1 Voltage = %0.2f  \tRV2 Voltage = %0.2f  \tRV3 Voltage = %0.2f",
    ADC_GetChannelTemp(ADC_Channel_4);
    Get_ADValue(&st_RoomSnr, ADC_Channel_4);
    u8_ADSampleCnt++;
    if (u8_ADSampleCnt >= CON_FilterNum)
    {
      u8_ADSampleCnt = 0;
      st_RoomSnr.u16_AvgAd = (st_RoomSnr.u16_SumAd - st_RoomSnr.u16_MaxAd - st_RoomSnr.u16_MinAd) / (CON_FilterNum - 2);

      u8_TempTableIndex = look_up_table((uint16_t *)ADRoomTemp, AD_TEMP_TABLE_SIZE, st_RoomSnr.u16_AvgAd);

      u16_stADRoomTemp = ADRoomTemp[u8_TempTableIndex];
      s8_RealTemp = RealTemp[u8_TempTableIndex];
      s8_RealTemp += (s8_TempComp_C7 / 10);
    }
  }
}

/***********************************************************************************************************************
***********************************************************************************************************************/

void Get_ADValue(ADSample_st *point, uint8_t u8_ADChannel)
{
  uint16_t u16_ADValue = 0;

  u16_ADValue = u16_AdcValue[u8_ADChannel];
  // u16_ADValue = u16_ADValue >> 2;
  //  u16_ADValue = 200;		//test
  if (u8_ADSampleCnt == 0)
  {
    point->u16_SumAd = u16_ADValue;
    point->u16_MaxAd = u16_ADValue;
    point->u16_MinAd = u16_ADValue;
  }
  else
  {
    point->u16_SumAd = point->u16_SumAd + u16_ADValue;
  }

  if (u16_ADValue > point->u16_MaxAd)
  {
    point->u16_MaxAd = u16_ADValue;
  }

  if (u16_ADValue < point->u16_MinAd)
  {
    point->u16_MinAd = u16_ADValue;
  }
}

// uint16_t CheckRoomTable(uint16_t u16_AdValue)
// {
//   if (u16_AdValue > 896u)
//   {
//     return (80u);
//   }
//   else if (u16_AdValue < 134u)
//   {
//     return (880u);
//   }
//   else
//   {
//     return (ADRoomTemp[(unsigned int)(u16_AdValue - 134u)]);
//   }
// }

// 查表函数
uint16_t look_up_table(uint16_t *a, uint16_t ArrayLong, uint16_t data)
{
  uint16_t begin, end, middle;
  uint16_t i;
  begin = 0;
  end = ArrayLong - 1;
  i = 0;
  if (data >= a[begin])
    return begin;
  else if (data <= a[end])
    return end;
  while (begin < end)
  {
    middle = (begin + end) / 2;
    if (data == a[middle])
      break;
    if (data < a[middle] && data > a[middle + 1])
      break;
    if (data > a[middle])
      end = middle;
    else
      begin = middle;
    if (i++ > ArrayLong)
      break;
  }
  if (begin > end)
    return 0;
  return middle;
}

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

/********************************************** (C) Copyright MindMotion **********************************************/
